package com.gree.manager.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gree.commom.result.FileResult;
import com.gree.commom.result.RespBean;
import com.gree.manager.entity.Brand;
import com.gree.manager.entity.Goods;
import com.gree.manager.entity.GoodsCategory;
import com.gree.manager.entity.GoodsImages;
import com.gree.manager.service.*;
import com.gree.manager.vo.GoodsCategoryVo;
import com.gree.commom.result.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 商品管理
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsImagesService goodsImagesService;

    @Autowired
    private UploadService uploadService;

    /**
     * 商品分类-列表页
     * @return
     */
    @GetMapping("/category/list")
    public String categoryList(Model model) throws JsonProcessingException {
        List<GoodsCategoryVo> list = goodsCategoryService.selectCategoryListForView();
        model.addAttribute("gcvList",list);
        return "goods/category/category-list";
    }

    /**
     * 商品分类-新增页
     * @return
     */
    @RequestMapping("/category/add")
    public String categoryAdd(Model model){
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",goodsCategoryList);
        return "goods/category/category-add";
    }

    /**
     * 商品分类-新增分类-级联查询
     * @param parentId
     * @return
     */
    @RequestMapping("/category/{parentId}")
    @ResponseBody
    public List<GoodsCategory> selectCategoryList(@PathVariable Short parentId) {
        return goodsCategoryService.selectCategoryByParentId(parentId);
    }

    /**
     * 商品分类-新增分类-保存分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping("/category/save")
    @ResponseBody
    public BaseResult categorySave(GoodsCategory goodsCategory) {
        int result = goodsCategoryService.categorySave(goodsCategory);
        return result > 0 ? BaseResult.success() : BaseResult.error();
    }

    /**
     * 商品-列表-页面跳转
     * @return
     */
    @RequestMapping("/list")
    public String goodsList(Model model){
        //返回所有商品分类
        model.addAttribute("gcList",goodsCategoryService.selectCategoryList());
        //返回所有商品品牌
        model.addAttribute("brandList",brandService.selectBrandList());
        return "goods/goods-list";
    }

    /**
     * 商品-新增-页面跳转
     * @return
     */
    @RequestMapping("/add")
    public String goodsAdd(Model model){
        List<GoodsCategory> gcList = goodsCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",gcList);
        List<Brand> brandList = brandService.selectBrandList();
        model.addAttribute("brandList",brandList);
        return "/goods/goods-add";
    }

    /**
     * 商品新增-保存
     * @param goods
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public BaseResult saveGoods(Goods goods){
        return goodsService.saveGoods(goods);
    }

    /**
     * 商品相册-保存
     * @param file
     * @param goodsId
     * @return
     * @throws IOException
     */
    @RequestMapping("/images/save")
    @ResponseBody
    public BaseResult saveGoodsImages(MultipartFile file, Integer goodsId) throws IOException {
        String filename = file.getOriginalFilename();
        String date = DateTimeFormatter.ofPattern("yyyy/MM/dd/").format(LocalDateTime.now());
        filename = date+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
        FileResult result = uploadService.upload(file.getInputStream(), filename);
        //上传成功
        if (!StringUtils.isEmpty(result.getFileUrl())){
            GoodsImages goodsImages = new GoodsImages();
            goodsImages.setImageUrl(result.getFileUrl());
            goodsImages.setGoodsId(goodsId);
            BaseResult baseResult = goodsImagesService.saveGoodsImages(goodsImages);
            return baseResult;
        }else {
            return BaseResult.error();
        }
    }

    /**
     * 商品列表-分页查询
     * @param goods
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/listForPage")
    @ResponseBody
    public BaseResult selectGoodsListByPage(Goods goods,Integer pageNum,Integer pageSize){
        return goodsService.selectGoodsListByPage(goods,pageNum,pageSize);
    }

    /**
     * 获取所有商品信息
     */
    @GetMapping("/listAll")
    @ResponseBody
    public List<Goods> getAllGoodsInfo(){
        return goodsService.getAllGoodsInfo();
    }
}
